package com.saifan.wyy_ov.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import com.saifan.wyy_ov.R;

/**
 * 浠嶴DCard寮傛锷犺浇锲剧墖
 *
 * @author wangli 2014-12-19 16:00:51
 */
public class SDCardImageLoader {
    //缂揿瓨
    private LruCache<String, Bitmap> imageCache;
    // 锲哄畾2涓嚎绋嬫潵镓ц浠诲姟
    private ExecutorService executorService = Executors.newFixedThreadPool(2);
    private Handler handler = new Handler();

    private int screenW, screenH;

    public SDCardImageLoader(int screenW, int screenH) {
        this.screenW = screenW;
        this.screenH = screenH;

        // 銮峰彇搴旗敤绋嫔簭链€澶у彲鐢ㄥ唴瀛?
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 8;

        // 璁剧疆锲剧墖缂揿瓨澶у皬涓虹▼搴忔渶澶у彲鐢ㄥ唴瀛樼殑1/8
        imageCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }

    private Bitmap loadDrawable(final int smallRate, final String filePath,
                                final ImageCallback callback) {
        // 濡傛灉缂揿瓨杩囧氨浠庣紦瀛树腑鍙栧嚭鏁版嵁
        if (imageCache.get(filePath) != null) {
            return imageCache.get(filePath);
        }

        // 濡傛灉缂揿瓨娌℃湁鍒栾鍙朣D鍗?
        executorService.submit(new Runnable() {
            public void run() {
                try {
                    BitmapFactory.Options opt = new BitmapFactory.Options();
                    opt.inJustDecodeBounds = true;
                    BitmapFactory.decodeFile(filePath, opt);

                    // 銮峰彇鍒拌繖涓浘鐗囩殑铡熷瀹藉害鍜岄佩搴?
                    int picWidth = opt.outWidth;
                    int picHeight = opt.outHeight;

                    //璇诲彇锲剧墖澶辫触镞剁洿鎺ヨ繑锲?
                    if (picWidth == 0 || picHeight == 0) {
                        return;
                    }

                    //鍒濆铡嬬缉姣斾緥
                    opt.inSampleSize = smallRate;
                    // 镙规嵁灞忕殑澶у皬鍜屽浘鐗囧ぇ灏忚绠楀嚭缂╂斁姣斾緥
                    if (picWidth > picHeight) {
                        if (picWidth > screenW)
                            opt.inSampleSize *= picWidth / screenW;
                    } else {
                        if (picHeight > screenH)
                            opt.inSampleSize *= picHeight / screenH;
                    }

                    //杩欐鍐岖湡姝ｅ湴鐢熸垚涓€涓湁镀忕礌镄勶紝缁忚绷缂╂斁浜嗙殑bitmap
                    opt.inJustDecodeBounds = false;
                    final Bitmap bmp = BitmapFactory.decodeFile(filePath, opt);
                    //瀛桦叆map
                    imageCache.put(filePath, bmp);

                    handler.post(new Runnable() {
                        public void run() {
                            callback.imageLoaded(bmp);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        return null;
    }

    /**
     * 寮傛璇诲彇SD鍗″浘鐗囷紝骞舵寜鎸囧畾镄勬瘮渚嬭繘琛屽帇缂╋纸链€澶т笉瓒呰绷灞忓箷镀忕礌鏁帮级
     *
     * @param smallRate 铡嬬缉姣斾緥锛屼笉铡嬬缉镞惰緭鍏?锛屾镞跺皢鎸夊睆骞曞儚绱犳暟杩涜杈揿嚭
     * @param filePath  锲剧墖鍦⊿D鍗＄殑鍏ㄨ矾寰?
     * @param imageView 缁勪欢
     */
    public void loadImage(int smallRate, final String filePath, final ImageView imageView) {

        Bitmap bmp = loadDrawable(smallRate, filePath, new ImageCallback() {

            @Override
            public void imageLoaded(Bitmap bmp) {
                if (imageView.getTag().equals(filePath)) {
                    if (bmp != null) {
                        imageView.setImageBitmap(bmp);
                    } else {
                        imageView.setImageResource(R.drawable.pic_none);
                    }
                }
            }
        });

        if (bmp != null) {
            if (imageView.getTag().equals(filePath)) {
                imageView.setImageBitmap(bmp);
            }
        } else {
            imageView.setImageResource(R.drawable.pic_none);
        }

    }


    // 瀵瑰鐣屽紑鏀剧殑锲炶皟鎺ュ彛
    public interface ImageCallback {
        // 娉ㄦ剰 姝ゆ柟娉曟槸鐢ㄦ潵璁剧疆鐩爣瀵硅薄镄勫浘镀忚祫婧?
        void imageLoaded(Bitmap imageDrawable);
    }
}
